package cn.tedu.oa.sys.service.impl;


import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.sys.dao.RoleDao;
import cn.tedu.oa.sys.dao.RoleMenuDao;
import cn.tedu.oa.sys.entity.Role;
import cn.tedu.oa.sys.entity.User;
import cn.tedu.oa.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Override
    public void update(Role role) {
        roleDao.update(role);
    }
    @Override
    public List<Role> getUserPagerList(String criteria, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
            throw new IllegalArgumentException("参数不合法，请联系运维人员");
        }

        int startIndex = (pageNo - 1) * pageSize + 1;
        int endIndex = startIndex + pageSize - 1;
        List<Role> list = roleDao.getRolePagerList(criteria, startIndex, endIndex);
        if (list == null) {
            throw new ServiceException("没有查询到数据");
        }
        return list;
    }

    @Override
    // 保存角色以及角色和菜单关系的数据

    public void insertMenuByRole(Integer roleId, Integer[] menuId, Integer isdel) {
        if(roleId==null)
            throw new IllegalArgumentException("角色信息不能为空");
        if(StringUtils.isEmpty(roleId.toString()))
            throw new IllegalArgumentException("角色不能为空");
        if(menuId==null||menuId.length==0)
            throw new IllegalArgumentException("必须为角色分配一个资源");
        //2.保存

        roleMenuDao.insertMenuByRole(
                roleId,
                menuId,
                isdel);
    }

    @Override
    public void add(Role role) {
        if (role == null) {
            throw new ServiceException("添加失败：用户信息为空");
        }
        if (StringUtils.isEmpty(role.getRoleName().trim())) {
            throw new ServiceException("添加失败：用户名为空");
        }

        if (StringUtils.isEmpty(role.getRoleDesc())) {
            throw new ServiceException("添加失败：描述为空");
        }

        /*添加人*/
        role.setAddUser(ShiroUtils.getPrincipal().getName());
        /*添加时间*/
        role.setAddTime(new Date());
        /*删除状态：0 正常 1 以删除*/
        role.setIsdel(0);
        roleDao.add(role);
    }
}

